<?php

namespace app\admin\controller;


class Notice extends Common {


    //通知公告表
    private $table_admin = "";

    /*
    *	构造函数
    */
    function __construct(){
        parent::__construct();
    }

    public function index(){
        $Notice = db('notice');
        $pageParam = ['query' =>[]];

        $where = [];
        $keyword = input('keyword');
        if( $keyword != ''){
            $where['title'] = array('like','%'.input('keyword').'%');
            $pageParam['query']['keyword'] = $keyword;
            $this->assign('keyword', $keyword);
        }

        $is_display = input('is_display');
        $this->assign('is_display', $is_display);
        if($is_display != ''){
            $where['is_display'] = input('is_display');
            $pageParam['query']['is_display'] = $is_display;

        }
        $data = $Notice->where($where)->order('id desc')->paginate(10, false, $pageParam);

        $this->assign('data',$data);
        return 	$this->fetch();
    }

    //通知的添加，修改
    public function add(){
        $Notice = db('notice');
        if(request()->isAjax()) {
            $title = input('title');
            $keyword = input('keyword')?input('keyword'):'公告';
            $content = input('content');
            $department = input('department');
            $state = input('state');
            $area[] = input('province');
            $area[] = input('city');
            $area[] = input('country');
            $areas = implode(',',$area);
            $file_num = input('file_num');
//            $add_content = input('add_content');
            $take_time = strtotime(input('take_time'));
            if($take_time == '0'){
                $take_time = time();
            }
            $over_time = strtotime(input('over_time'));
            $create_time = time();
            $is_display = input('is_display');
//            || !$keyword || !$content || !$department  || !$file_num || !$add_content || !$take_time || !$over_time
            if (!$title)
                $this->error('操作失败，有字段为空');

            $data['title'] = $title;
            $data['content'] = $content;
            $data['department'] = $department;
            $data['state'] = $state;
            $data['area'] = $areas;
            $data['file_num'] = $file_num;
//            $data['add_content'] = $add_content;
            $data['take_time'] = $take_time;
            $data['over_time'] = $over_time;
            $data['create_time'] = $create_time;
            $data['is_display'] = $is_display;

            $file = request()->file('file');

            if($file) {
                $info = $file->validate(['ext' => 'pdf'])->move(ROOT_PATH . 'public/static/uploads');

                if ($info) {
                    $file_name = input('file_name');
                    $file_path = $info->getSaveName();
                    $getInfo = $info->getInfo();
                    $data_file['file_name'] = $file_name;
                    $data_file['file_path'] = $file_path;
                    $data_file['file_time'] = time();
                    $file_suffix = substr(strrchr($file_path, '.'), 1);
                    $data_file['file_suffix'] = $file_suffix;
                    $data_file['file_size'] = $getInfo['size'];

                    db('file')->insert($data_file);
                    $file_id = db('file')->getLastInsID();
                    $data['file_id'] = $file_id;
                }
                //修改逻辑，修改附件，需删除原附件
                if(input('fid')){
                    $fid = input('fid');
                    $file = db('file')->where('id',$fid)->value('file_path');
                    $fileurl =  ROOT_PATH . 'public/static/uploads/'.$file;
                    $res = db('file')->where('id',$fid)->delete();
                    if($res && file_exists($fileurl)){
                        unlink($fileurl);
                    }
                }
            }

            //下载文件上传
            $file = request()->file('down_file');
            if($file){
                $info = $file->move(ROOT_PATH . 'public/static/uploads');

                if ($info) {
                    $down_file_name = input('down_file_name');
                    $file_path = $info->getSaveName();
                    $getInfo = $info->getInfo();
                    $data_file['file_name'] = $down_file_name;
                    $data_file['file_path'] = $file_path;
                    $data_file['file_time'] = time();
                    $file_suffix = substr(strrchr($file_path, '.'), 1);
                    $data_file['file_suffix'] = $file_suffix;
                    $data_file['file_size'] = $getInfo['size'];

                    db('file')->insert($data_file);
                    $down_id = db('file')->getLastInsID();
                    $data['down_id'] = $down_id;
                }

                //修改逻辑，修改附件，需删除原附件
                $did = input('did');
                if(!empty($did)){
                    $file_path = db('file')->where('id',$did)->value('file_path');
                    $fileurl = ROOT_PATH . 'public/static/uploads/'.$file_path;
                    $res = db('file')->where('id',$did)->delete();
                    if($res && file_exists($fileurl) && !empty($file_path)){
                        unlink($fileurl);
                    }
                }
            }
            if (!input('id')) {
//            添加
                $Notice->insert($data);
                $id = $Notice->getLastInsID();
                $dakey['notice_id'] = $id;
                $dakey['keywords'] = $keyword;
                db('keywords')->insert($dakey);
            } else {
//            修改
                $id = input('id');
                $Notice->where('id', $id)->update($data);
                $dakey['keywords'] = $keyword;
                db('keywords')->where('notice_id',$id)->update($dakey);
            }

            $this->success("操作成功!", url('Notice/index'));
        }
//        $data = $Notice->order('id','desc')->paginate(10);
        $province = $this->getProvince();
        $this->assign('province', $province);
        $this->assign('state', getState());
        return $this->fetch();
    }

    public function edit(){
        $id = input('id');
        $data = db('notice')->where('id',$id)->find();
        $area = explode(',',$data['area']);
        $keyword = db('keywords')->where('notice_id',$id)->value('keywords');
        $province = $this->getProvince();

        $this->assign('state', getState());
        $this->assign('data',$data);
        $this->assign('area',$area);
        $this->assign('keyword',$keyword);
        $this->assign('province', $province);
        return $this->fetch();
    }

    public function delete(){
        $id = input('id');
        $file = db('notice')->alias('p')
            ->join('file f','p.file_id = f.id','RIGHT')
            ->where('p.id',$id)->value('f.file_path');

        if($file){
            $url = ROOT_PATH . 'public/static/uploads/'.$file;
            if(!empty($file)){
                unlink($url);
            }

//        $sql = db('policy')->getLastSql();
//        $this->success($sql);
        }
        db('notice')->where('id',$id)->delete();
        db('keywords')->where('notice_id',$id)->delete();
        $this->success('操作成功！', url('Notice/index'));
    }

    //    获取省
    public function getProvince(){
        $data = db('area')->where('parent_id',0)->select();
        return $data;
    }

//    ajax获取area
    public function getAreas(){
        $id = input('id');
        if($id){
            $data = db('area')->where('parent_id',$id)->select();
            $msg = '';
            foreach ($data as $key => $val) {
                $msg .= '<option value="'.$val['id'].'">'.$val['name'].'</option>';
            }
        }
        $this->success($msg);
    }

}